package com.jjrwa.component;

import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public abstract class BaseFieldTable<T extends ITableDataEntity> extends JTable {

    protected TableDataHandle<T> handle;

    public BaseFieldTable(TableDataHandle<T> handle) {
        super(handle.getRows(), handle.getColumnNames());
        this.handle = handle;
        this.init();
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        if (handle.getNotEditableCells().contains(column)) {
            return false;
        }
        return super.isCellEditable(row, column);
    }

    protected void init() {
        this.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        this.setShowGrid(false);
        //第一列
        this.getColumnModel().getColumn(0).setCellRenderer(new CheckBoxRenderer());
        this.getColumnModel().getColumn(0).setCellEditor(new CheckBoxCellEditor());

        //最后一列为操作详情列
        IButtonEvent event = new IButtonEvent() {
            @Override
            public void invoke(ActionEvent e) {
                TableButton button = (TableButton) e.getSource();
                Messages.showErrorDialog("详情功能尚未开放!", "Under Construction");
            }
        };
        int lastColumnFlag = handle.getColumnNames().size() - 1;
        this.getColumnModel().getColumn(lastColumnFlag).setCellRenderer(new ButtonRender());
        this.getColumnModel().getColumn(lastColumnFlag).setCellEditor(new ButtonEditor(event));

        //设置列宽
        handle.setPreferredWidth(this);
    }


    protected class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor {

        protected JCheckBox checkBox;

        public CheckBoxCellEditor() {
            checkBox = new JCheckBox();
            checkBox.setHorizontalAlignment(SwingConstants.CENTER);
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            checkBox.setSelected(((Boolean) value).booleanValue());
            return checkBox;
        }

        @Override
        public Object getCellEditorValue() {
            return Boolean.valueOf(checkBox.isSelected());
        }
    }

    protected class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

        public CheckBoxRenderer() {
            super();
            setOpaque(true);
            setHorizontalAlignment(SwingConstants.CENTER);
        }

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {

            if (value instanceof Boolean) {
                setSelected(((Boolean) value).booleanValue());
                setEnabled(table.isCellEditable(row, column));
                setForeground(table.getForeground());
                setBackground(table.getBackground());
            }
            return this;
        }
    }

    protected interface IButtonEvent {
        void invoke(ActionEvent e);
    }

    protected class TableButton extends JButton {
        private int row;

        public TableButton(String text) {
            super(text);
        }

        public int getRow() {
            return row;
        }

        public void setRow(int row) {
            this.row = row;
        }
    }

    protected class ButtonRender implements TableCellRenderer {
        private JButton button;

        public ButtonRender() {
            button = new JButton("=");
        }

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            return button;
        }
    }

    protected class ButtonEditor extends DefaultCellEditor {

        private TableButton button;

        public ButtonEditor(IButtonEvent event) {
            super(new JTextField());
            button = new TableButton("=");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    event.invoke(e);
                }
            });
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value,
                                                     boolean isSelected, int row, int column) {
            setClickCountToStart(0);
            button.setRow(row);
            return button;
        }
    }
}
